<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form class="search" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="名称" prop="remarks">
            <el-input v-model="queryParams.remarks" placeholder="请输入名称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>

          <el-form-item label="库类型" prop="sjklx">
            <el-select v-model="queryParams.sjklx" placeholder="请选择数据库类型" clearable size="small">
              <el-option v-for="dict in dict.type.sjklx" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="项目" prop="classificationId">
            <el-select v-model="queryParams.classificationId" placeholder="请选择项目" clearable size="small">
              <el-option v-for="item in classificationOptions" :key="item.id" :label="item.classificationName"
                :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['database:database:add']">新增</el-button>
          </el-col>

          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['database:database:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['database:database:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table  @cell-dblclick="celldblclick" v-loading="loading" :data="databaseList"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column label="名称" align="left" prop="remarks"></el-table-column>
          <el-table-column label="账号" align="left" prop="username">
            <template slot-scope="scope">
              <el-tooltip class="pointer" trigger="hover" content="双击复制" placement="right">
                <el-tag size="medium">{{ scope.row.username }}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="密码" align="left" prop="password">
            <template slot-scope="scope">
              <el-tooltip  class="pointer" trigger="hover" content="双击复制" placement="right">
                <el-tag size="medium">{{ scope.row.password.replace(/^(.).*(.)$/,"$1***$2") }}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="ip" align="left" prop="website">
            <template slot-scope="scope">
              <el-tooltip class="pointer" trigger="hover" content="双击复制" placement="right">
                <el-tag size="medium">{{ scope.row.website }}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column  class="pointer" label="端口" align="center" prop="dk">
            <template slot-scope="scope">
              <el-tooltip class="pointer" trigger="hover" content="双击复制" placement="right">
                <el-tag size="medium">{{ scope.row.dk }}</el-tag>
              </el-tooltip>
            </template>

          </el-table-column>

          <el-table-column label="类型" align="center" prop="sjklx">
            <template slot-scope="scope">
              <dict-tag   :options="dict.type.sjklx" :value="scope.row.sjklx" />
            </template>

          </el-table-column>
          <el-table-column label="项目分类" align="center" prop="classificationName"></el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-copy-document" @click="copyRow(scope.row)">
                复制</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['database:database:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['database:database:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList"> </pagination>
      </el-col>
    </el-row>
    <!-- 添加或修改数据库账号管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入名称" />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="账号" prop="username">
                <el-input v-model="form.username" prefix-icon="el-icon-user" placeholder="请输入数据库账号" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据密码" prop="password">
              <el-input v-model="form.password" :show-password="true" prefix-icon="el-icon-lock"
                placeholder="请输入数据密码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="域名" prop="yuming">
                <el-input v-model="form.yuming" placeholder="请输入域名" />
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="website">
              <el-input v-model="form.website" placeholder="请输入连接地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label="项目" prop="classificationId">
                <el-select v-model="form.classificationId" placeholder="请选择项目">
                  <el-option v-for="item in classificationOptions" :key="item.id" :label="item.classificationName"
                    :value="item.id" />
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库类型" prop="sjklx">
              <el-select v-model="form.sjklx" placeholder="请选择数据库类型">
                <el-option v-for="dict in  dict.type.sjklx" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="端口" prop="dk">
              <el-input v-model="form.dk" placeholder="请输入端口" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" show-word-limit :rows="5" maxlength="200" v-model="form.remark"
            placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    listDatabase,
    getDatabase,
    addDatabase,
    delDatabase,
    updateDatabase
  } from "@/api/admin/manager/database";
  import Treeselect from "@riophae/vue-treeselect";
  import { listAllClassification } from "@/api/project/classification";
  
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    dicts: ['sjklx'],
    components: { Treeselect },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 数据库账号管理表格数据
        databaseList: [],
        // 项目分类选项
        classificationOptions: [],

        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 数据库类型字典
        sjklxOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          remarks: null,
          classificationId: null,

          sjklx: null,
          dk: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          remarks: [{
            required: true,
            message: "名称不能为空",
            trigger: "blur"
          }],

          username: [{
            required: true,
            message: "账号不能为空",
            trigger: "blur"
          }],

          password: [{
            required: true,
            message: "密码不能为空",
            trigger: "blur"
          }],
          sjklx: [{
            required: true,
            message: "数据库类型不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.getList();
      this.getClassificationOptions();

    },
    methods: {
      /** 查询项目分类下拉选项 */
      getClassificationOptions() {
        listAllClassification().then(response => {
          this.classificationOptions = response.data;
        });
      },

      /** 查询数据库账号管理列表 */
      getList() {
        this.loading = true;
        listDatabase(this.queryParams).then(response => {
          if (response.code === 200) {
            this.databaseList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        });
      },
      celldblclick(row, column, cell, event) {
        if (column.property == "username" || column.property == "website" || column.property ==
          "password" ||
          column.property == "dk") {
          this.$copyText(row[column.property]).then(e => {
            this.copy()
          }, function(e) {
            this.error()
          })
        }

      },
      copyRow(row) {
        let data = "账号：" + row.username + " 密码：" + row.password + " 网址：" + row.website + " 端口：" +
          row.dk
        this.$copyText(data).then(e => {
          this.copy()
        }, function(e) {
          this.error()
        })
      },
      copy() {
        this.$notify({
          title: '成功',
          message: '复制成功！',
          type: 'success',
          offset: 50,
          duration: 2000
        })
      },
      error() {
        this.$notify({
          title: '失败',
          message: '复制失败！',
          type: 'error',
          offset: 50,
          duration: 2000
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          userId: null,
          remarks: null,
          yuming: null,
          username: null,
          password: null,
          website: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          classificationId: null,
          sjklx: null,
          dk: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加数据库账号管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDatabase(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改数据库";
        });

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateDatabase(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });

            } else {
              addDatabase(this.form).then(response => {
               this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除数据库账号管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDatabase(ids)
        }).then(() => {
          debugger
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有数据库账号管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return $.get(alluse.bigcenter + api.导出, this.queryParams,
            (response) => {
              if (response.code === 200) {

              }
            });
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  }
</script>
